package cn.com.dhcc.app.pub.core.listener;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.FtpReply;
import org.apache.ftpserver.ftplet.FtpRequest;
import org.apache.ftpserver.ftplet.FtpSession;
import org.apache.ftpserver.ftplet.Ftplet;
import org.apache.ftpserver.ftplet.FtpletContext;
import org.apache.ftpserver.ftplet.FtpletResult;
import org.apache.log4j.Logger;

import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.util.CollectionUtil;
import cn.com.dhcc.turbo.service.monitor.FtpOnlineUser;

import com.tongtech.backport.java.util.Arrays;

/**
 * 对FTP上传下载进行监控
 */
public class FtpletListener implements Ftplet {

	private static Map<String, FtpOnlineUser> loginUsers = new HashMap<String, FtpOnlineUser>();
	public static Date startTime = null;
	@SuppressWarnings("deprecation")
	Logger logger = Log.getDetachLogger(LOGTYPE.FTP);
	public static Map<String, FtpOnlineUser> getLoginUsers(){
		return loginUsers;
	}
	@Override
	public void init(FtpletContext paramFtpletContext) throws FtpException {
		StringBuffer ftpStat = new StringBuffer();
		ftpStat.append("已创建用户："+Arrays.toString(paramFtpletContext.getUserManager().getAllUserNames()));
		logger.info("FTP:监听已启动。当前信息:"+ftpStat.toString());
		startTime = new Date();
	}

	@Override
	public void destroy() {
		logger.warn("FTP已关闭...");
	}

	@Override
	public FtpletResult onConnect(FtpSession paramFtpSession) throws FtpException, IOException {
		logger.error(paramFtpSession.getClientAddress() + "FTP:客户端已连接"+paramFtpSession.getServerAddress()  
			+ " ||" + paramFtpSession.getDataType().name());
		return FtpletResult.DEFAULT;
	}

	@Override
	public FtpletResult onDisconnect(FtpSession session) throws FtpException, IOException {
		if (session!=null && session.getUser()!=null) {
			logger.error(session.getClientAddress() + "FTP:客户端已断开"+session.getServerAddress());
			loginUsers.remove(session.getUser().getName());
		}
		CollectionUtil.printMap(loginUsers);
		return FtpletResult.DEFAULT;
	}

	@Override
	public FtpletResult beforeCommand(
			FtpSession session, 
			FtpRequest request) throws FtpException, IOException {
		return null;
	}

	@Override
	public FtpletResult afterCommand(
			FtpSession session, 
			FtpRequest request, 
			FtpReply paramFtpReply)
			throws FtpException, IOException {
		return null;
	}

	public FtpletResult onLogin(FtpSession session, FtpRequest request) throws FtpException, IOException {
		logger.info("FTP:用户["+session.getUser().getName()+"]登录成功"+request.getCommand());
		String userName = session.getUser().getName();
		Date loginTime = new Date();
		String client = session.getClientAddress().toString();
		loginUsers.put(session.getUser().getName(), new FtpOnlineUser(userName,loginTime,client));
		CollectionUtil.printMap(loginUsers);
		return null;
	}

	public FtpletResult onDeleteStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
		return null;
	}

	public FtpletResult onDeleteEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
		return null;
	}

	public FtpletResult onUploadStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
		logger.error("onUploadStart=======" + request.getCommand() + "||" + session.getClientAddress().getAddress());
		return null;
	}

	public FtpletResult onUploadEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
		logger.error("onUploadEnd=======" + request.getCommand() + "||" + session.getClientAddress().getAddress());
		return null;
	}

	public FtpletResult onDownloadStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
		return null;
	}

	public FtpletResult onDownloadEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
		return null;
	}

	public FtpletResult onRmdirStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
		return null;
	}

	public FtpletResult onRmdirEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
		return null;
	}

	public FtpletResult onMkdirStart(FtpSession session, FtpRequest request) throws FtpException, IOException {

        return null;
	}

	public FtpletResult onMkdirEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
        return null;
	}

	public FtpletResult onAppendStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
		return null;
	}

	public FtpletResult onAppendEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
		return null;
	}

	public FtpletResult onUploadUniqueStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
		logger.error("onUploadUniqueStart=======" + request.getCommand() + "||" + session.getClientAddress().getAddress());
		return null;
	}

	public FtpletResult onUploadUniqueEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
		logger.error("onUploadUniqueEnd=======" + request.getCommand() + "||" + session.getClientAddress().getAddress());
		return null;
	}

	public FtpletResult onRenameStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
		return null;
	}

	public FtpletResult onRenameEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
		return null;
	}

	public FtpletResult onSite(FtpSession session, FtpRequest request) throws FtpException, IOException {
		return null;
	}
}